package com.study.common.interceptor;

import com.study.common.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author 谭永超 [www.tanyongchao.tk]
 * @Date 2018/7/19 23:22
 * @Description 拦截器拦截为登陆的后台访问
 */
public class LoginInterceptor implements HandlerInterceptor {

    //Pattern java正则表达式类
    @Value("$(serverurl)")
    private String serverurl;
    /**
     * 不需要过滤的地址
     *
     * @Author 谭永超 [www.tanyongchao.tk]
     * @Date 2018/8/28 22:32
     * @Description
     */
    protected static List<Pattern> patterns = new ArrayList<Pattern>();

    private static void notInterceptUrl() {
        patterns.add(Pattern.compile("login"));
        patterns.add(Pattern.compile("util/.*"));
        patterns.add(Pattern.compile("img/.*"));
        patterns.add(Pattern.compile("register/.*"));
        patterns.add(Pattern.compile(".idea/.*"));
        patterns.add(Pattern.compile(".*[(\\.js)||(\\.css)||(\\.png)||(\\.html)||(\\.ico)]"));
    }

    /**
     * @Author 谭永超 [www.tanyongchao.tk]
     * @Date 2018/7/28 17:56
     * @Description 静态块加载patterns 正则类的初始信息
     */
    static {
        notInterceptUrl();
    }

    /**
     * @Author 谭永超 [www.tanyongchao.tk]
     * @Date 2018/7/28 17:55
     * @Description 判断是否能通过拦截器
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        获取uri
        String uri = request.getRequestURI();
        System.out.println(uri);
//        判断uri中是否有login字段 安全
        String url = uri.substring(request.getContextPath().length());
        if (url.startsWith("/") && url.length() > 1) {
            url = url.substring(1);
        }
        if (isInclude(url)) {
            return true;
        } else {
            //判断换user中是否有值
            User user = (User) request.getSession().getAttribute("userInfo");
            if (user != null) {
                return true;
            } else {
                //转发
                request.getSession().setAttribute("remind", "请先登陆");
                request.getRequestDispatcher("/demo/login").forward(request, response);
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }

    /**
     * 是否需要过滤
     *
     * @param url
     * @return
     */
    private boolean isInclude(String url) {
        for (Pattern pattern : patterns) {
            Matcher matcher = pattern.matcher(url);
            if (matcher.matches()) {
                return true;
            }
        }
        return false;
    }

}
